<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户编号" prop="lesseeId">
        <el-input
          v-model="queryParams.lesseeId"
          placeholder="请输入商户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付金额" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入支付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="paymentTime">
        <el-date-picker clearable
          v-model="queryParams.paymentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['annuity:annuity:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['annuity:annuity:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['annuity:annuity:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['annuity:annuity:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="annuityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="缴费信息编号" align="center" prop="annuityRecordId" />
      <el-table-column label="商户编号" align="center" prop="lesseeId" />
      <el-table-column label="支付渠道" align="center" prop="payType" />
      <el-table-column label="支付金额" align="center" prop="price" />
      <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['annuity:annuity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['annuity:annuity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改年费信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户编号" prop="lesseeId">
          <el-input v-model="form.lesseeId" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="支付金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paymentTime">
          <el-date-picker clearable
            v-model="form.paymentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">租户信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTbLessee">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTbLessee">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tbLesseeList" :row-class-name="rowTbLesseeIndex" @selection-change="handleTbLesseeSelectionChange" ref="tbLessee">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="商户名称" prop="lesseeName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.lesseeName" placeholder="请输入商户名称" />
            </template>
          </el-table-column>
          <el-table-column label="法人名称" prop="signName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.signName" placeholder="请输入法人名称" />
            </template>
          </el-table-column>
          <el-table-column label="法人身份证号" prop="identityCard" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.identityCard" placeholder="请输入法人身份证号" />
            </template>
          </el-table-column>
          <el-table-column label="联系方式" prop="lesseePhone" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.lesseePhone" placeholder="请输入联系方式" />
            </template>
          </el-table-column>
          <el-table-column label="注册地址" prop="lesseeAddr" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.lesseeAddr" placeholder="请输入注册地址" />
            </template>
          </el-table-column>
          <el-table-column label="付费模式" prop="lesseePolicy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.lesseePolicy" placeholder="请输入付费模式" />
            </template>
          </el-table-column>
          <el-table-column label="审批通过时间" prop="approvalTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.approvalTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择审批通过时间" />
            </template>
          </el-table-column>
          <el-table-column label="到期时间" prop="endTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择到期时间" />
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="state" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.state" placeholder="请输入状态" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAnnuity, getAnnuity, delAnnuity, addAnnuity, updateAnnuity } from "@/api/annuity/annuity";

export default {
  name: "Annuity",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbLessee: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 年费信息表格数据
      annuityList: [],
      // 租户表格数据
      tbLesseeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lesseeId: null,
        payType: null,
        price: null,
        paymentTime: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lesseeId: [
          { required: true, message: "商户编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询年费信息列表 */
    getList() {
      this.loading = true;
      listAnnuity(this.queryParams).then(response => {
        this.annuityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        annuityRecordId: null,
        lesseeId: null,
        payType: null,
        price: null,
        paymentTime: null,
        status: null
      };
      this.tbLesseeList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.annuityRecordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加年费信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const annuityRecordId = row.annuityRecordId || this.ids
      getAnnuity(annuityRecordId).then(response => {
        this.form = response.data;
        this.tbLesseeList = response.data.tbLesseeList;
        this.open = true;
        this.title = "修改年费信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbLesseeList = this.tbLesseeList;
          if (this.form.annuityRecordId != null) {
            updateAnnuity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAnnuity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const annuityRecordIds = row.annuityRecordId || this.ids;
      this.$modal.confirm('是否确认删除年费信息编号为"' + annuityRecordIds + '"的数据项？').then(function() {
        return delAnnuity(annuityRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 租户序号 */
    rowTbLesseeIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 租户添加按钮操作 */
    handleAddTbLessee() {
      let obj = {};
      obj.lesseeName = "";
      obj.signName = "";
      obj.identityCard = "";
      obj.lesseePhone = "";
      obj.lesseeAddr = "";
      obj.lesseePolicy = "";
      obj.approvalTime = "";
      obj.endTime = "";
      obj.state = "";
      this.tbLesseeList.push(obj);
    },
    /** 租户删除按钮操作 */
    handleDeleteTbLessee() {
      if (this.checkedTbLessee.length == 0) {
        this.$modal.msgError("请先选择要删除的租户数据");
      } else {
        const tbLesseeList = this.tbLesseeList;
        const checkedTbLessee = this.checkedTbLessee;
        this.tbLesseeList = tbLesseeList.filter(function(item) {
          return checkedTbLessee.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbLesseeSelectionChange(selection) {
      this.checkedTbLessee = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('annuity/annuity/export', {
        ...this.queryParams
      }, `annuity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
